package com.servlet;

import com.entity.Page;
import com.entity.UserCriteria;
import com.service.PageSearchService;
import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/bookBorrow")
public class BookBorrowServlet extends HttpServlet {
    PageSearchService pageSearchService;
    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        this.pageSearchService = new PageSearchService();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        request.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

        String name = request.getParameter("name");
        String typeOrAuthor = request.getParameter("typeOrAuthor");
        String conditionSort = request.getParameter("conditionSort");
        UserCriteria userCriteria = new UserCriteria();
        userCriteria.setName(name);
        userCriteria.setUsertypeOrAuthor(typeOrAuthor);
        userCriteria.setSortBy("id");
        userCriteria.setSortDirection(conditionSort);
        int pageNumber = 1;
        if(request.getParameter("pageNumber") != null) {
            try {
                pageNumber=Integer.parseInt(request.getParameter("pageNumber")) ;
            } catch (NumberFormatException e) {
                throw new RuntimeException(e);
            }
        }
        if(pageNumber<=0)
        {
            pageNumber=1;
        }
        Page p = null;
        try {
            userCriteria.setPageNumber(pageNumber);
            p = pageSearchService.getPageBySearch("Operatebook", userCriteria);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        if(p.getTotalPage()==0)
        {
            p.setTotalPage(1);
            p.setPageNumber(1);
        }
        else {
            if(pageNumber>=p.getTotalPage()+1)
            {
                try {
                    userCriteria.setPageNumber(pageNumber);
                    p = pageSearchService.getPageBySearch("Operatebook", userCriteria);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
        if(p.getData().size()<1){
            try {
                userCriteria.setPageNumber(pageNumber-1);
                p = new PageSearchService().getPageBySearch("Users", userCriteria);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        request.setAttribute("name",name);
        request.setAttribute("typeOrAuthor",typeOrAuthor);
        request.setAttribute("conditionSort",conditionSort);
        request.setAttribute("p", p);
        request.getRequestDispatcher("/admin/borrow_message.jsp").forward(request, resp);
    }
}
